;
(function (window, angular) {
    'use strict';
    angular.module("Controller.BasicData.SafeInventoryConfig", [])
        .controller("SafeInventoryConfigCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", "EventBus", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService, EventBus) {
            $scope.activeFlag = [true, false, false];
            $scope.submitted = false;
            $scope.dataList = [];
            $scope.detailInfo = {};
            $scope.tabFlag = {};
            var tabFlag = {
                0: "新增",
                1: "编辑"
            };
            $scope.dataObj = {};

            var btnPermissionCode = {
                safeInventoryConfigEidt: window.ActionCode.safeInventoryConfigEidt
            };

            $scope.btnPermission = {
                safeInventoryConfigEidt: true
            };

            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                switch (index) {
                    case 0:
                        initData();
                        break;
                    case 1:
                        break;
                    case 2:
                        break;
                    default:
                        break;
                }
            };
            function initData() {
                $scope.tabFlag = {};
            }
            $scope.getData = function () {
                var service = "device";
                var reqUrl = "/comSafetyStock/item";
                ybHttp.Post(service, reqUrl).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.dataObj = resp.value;
                    if($scope.dataObj ){
                        $scope.dataObj.calStr = $filter("TrueOrFalse")($scope.dataObj.calFlag);
                    }

                })
            };

            $scope.addData = function () {
                $scope.tabFlag.type = 0;
                $scope.tabFlag.name = tabFlag[0];
                $scope.dataObj = {
                    planCode: 1,
                    calFlag: 0
                };
                $scope.changeTab(1);
            };

            $scope.editData = function (data) {
                $scope.tabFlag.type = 1;
                $scope.tabFlag.name = tabFlag[1];
                $scope.detailInfo = {
                    planCode: data.planCode || "",
                    calFlag: data.calFlag || 0
                };
                $scope.changeTab(1);
            };

            $scope.saveData = function (isValid) {
                if (!isValid) {
                    $scope.submitted = true;
                    return;
                }
                var service = "device";
                var reqUrl = "/comSafetyStock/save";
                if ($scope.tabFlag.type === 1) {
                    reqUrl = "/comSafetyStock/update";
                }

                var param = angular.copy($scope.dataObj);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    ybWidget.ShowToaster('success', '操作成功');
                    $scope.changeTab(0);
                    $scope.getData();
                });
            };

            (function () {
                $scope.getData();
                UserContextService.SetBtnPermission(setBtnPermission);
            })();
        }])
    ;
})(window, window.angular);